package com.game;

import com.game.Block;
import com.game.Char;
import com.game.World;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;


public class WorldRenderer {
	
	private static final float CAMERA_WIDTH = 10f;
	private static final float CAMERA_HEIGHT = 7f;
	
	private World world;
	private OrthographicCamera cam;
	
	ShapeRenderer debugRenderer = new ShapeRenderer();
	
	private Texture charTexture;
	
	private SpriteBatch batch;
	private boolean debug = false;
	private int width, height;
	private float ppuX, ppuY;
	public void setSize(int w, int h){
		this.width = w;
		this.height = h;
		ppuX = (float)width / CAMERA_WIDTH;
		ppuY = (float)height / CAMERA_HEIGHT;
		
	}
	
	public void loadTextures(){
		charTexture = new Texture(Gdx.files.internal("data/char.png"));
		Gdx.app.log("what", "null");
	}
	
	public void drawChar(){
		Char player = world.getPlayer();
		batch.draw(charTexture, player.getPosition().x*ppuX, player.getPosition().y*ppuY, player.SIZE*ppuX, player.SIZE*ppuY);
	}
	
	public WorldRenderer(World world, boolean debug){
		this.world = world;
		this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
		this.cam.position.set(CAMERA_WIDTH/2f, CAMERA_HEIGHT/2f, 0);
		this.cam.update();
		this.debug = debug;
		batch = new SpriteBatch();
		loadTextures();
	}
	
	public void render(){
		debugRenderer.setProjectionMatrix(cam.combined);
		debugRenderer.begin(ShapeType.Rectangle);
		for( Block block : world.getBlocks()){
			Rectangle rect = block.getBounds();
			float x1 = block.getPosition().x + rect.x;
			float y1 = block.getPosition().y + rect.y;
			debugRenderer.setColor(new Color(1, 0, 0, 1));
			debugRenderer.rect(x1, y1, rect.width, rect.height);
		}
		if(debug){
			Char player = world.getPlayer();
			Rectangle rect = player.getBounds();
			float x1 = player.getPosition().x + rect.x;
			float y1 = player.getPosition().y + rect.y;
			debugRenderer.setColor(new Color(0, 1, 0, 1));
			debugRenderer.rect(x1, y1, rect.width, rect.height);
			debugRenderer.end();
		}
		else{
			debugRenderer.end();
			batch.begin();
			drawChar();
			batch.end();
		}

	}
	

}
